<?php
/**
 * Created by PhpStorm.
 * User: lixy
 * Date: 2015/12/21
 * Time: 20:29
 */

namespace app\controllers;


use yii\web\Controller;
use Yii;
use app\models\weixin\T_WXMALL_USER;
use app\models\T_WX_PAY_LOG;
use app\lib\WeixinUtils;
use app\lib\Mch_pay;
use app\lib\wxpay\WxPayApi;
class HomeController extends Controller
{
    Const PWD='bbc';
    public $layout=false;
    public $enableCsrfValidation = false;
    public function beforeAction($action)
    {
        $request = Yii::$app->request;
        $u=$request->get("u");
        if($u!=null && $u==static::PWD){
          return true;
        }
        echo "不允许用此功能";
        return false;
    }
    //编辑菜单 home/menu-edit?u=bbc
    public function actionMenuEdit(){
        $request = Yii::$app->request;

        if($request->post('menuStr')!=null){
//            var_dump($request->post('menuStr'));
            var_dump( WeixinUtils::_createMenu(json_decode($request->post('menuStr'))));
            exit;
        }
        $re=WeixinUtils::_getMenu();
        $menujson= json_encode($re->menu, JSON_UNESCAPED_UNICODE);
        // var_dump( $re->selfmenu_info);
        return $this->render('menu-edit',['menujson'=>$menujson]);
    }
    public function actionIndex(){
        //模板消息测试
       $re=WeixinUtils::sendTemplateMsg('PlaoOPIj1YS3CaxUhJ6Fy5SPSuWjVwFcggmlhBq2PuQ','oTJVss0fVh-xou1H-tVyNnijmDyk',
           'www.baidu.com',[
               'first'=>['value'=>'你的订单物流信息有变化','color'=>'#173177'],
               'order_code'=>['value'=>'你的订单物流信息有变化','color'=>'#173177'],
               'time'=>['value'=>'你的订单物流信息有变化','color'=>'#173177'],
               'wldc'=>['value'=>'你的订单物流信息有变化','color'=>'#173177'],
               'remark'=>['value'=>'你的订单物流信息有变化','color'=>'#173177'],

           ]);
        var_dump($re);
        exit;
//        {{first.DATA}} 订单号：{{order_code.DATA}} 时间：{{time.DATA}} 物流公司：{{wldc.DATA}} {{remark.DATA}}
    }


    //http://wx.duolahe.com/biz_weixin/web/index.php/home/create-menus
    public function actionCreateMenus(){
        $menus=["button"=>[
			
            ["name"=>"朵拉商城","sub_button"=>[
                ["type"=>"view","name"=>"龙珠礼包","url"=>"http://wx.duolahe.com/biz_weixin/web/index.php/special/dragon-ball"],
                ["type"=>"view","name"=>"新春礼包","url"=>"http://wx.duolahe.com/biz_weixin/web/index.php/wx-bag/home"],
            ]],
            ["type"=>"view","name"=>"企业频道","url"=>"http://u.liveapp.cn/804841"],
			["name"=>"个人中心","sub_button"=>[
                ["type"=>"view","name"=>"送礼记录","url"=>"http://wx.duolahe.com/biz_weixin/web/index.php/wx-home/send-record"],
                ["type"=>"view","name"=>"收礼记录","url"=>"http://wx.duolahe.com/biz_weixin/web/index.php/wx-home/receive-record"],
                ["type"=>"view","name"=>"我的朵拉币","url"=>"http://wx.duolahe.com/biz_weixin/web/index.php/wx-home/doracoin-record"],
				["type"=>"view","name"=>"绑定手机","url"=>"http://wx.duolahe.com/biz_weixin/web/index.php/wx-biz/user-center"],
				["type"=>"click","name"=>"客服热线","key"=>"400"],
            ]],
            

        ]];
        var_dump( WeixinUtils::_createMenu($menus));
    }



    public function actionTest($code=123){
        $key = 'web_token_data';
        $userinfo = WeixinUtils::weixin_userinfo_filter();
        if ($userinfo == null) {
            echo "验证失败";
         exit;
        }
        $session = Yii::$app->session;
        $tokendata = $session->get($key);
        var_dump($tokendata);
        echo "验证成功";
    }
    //移除session
    public function actionRemoveS(){
        $session = Yii::$app->session;
        $key = 'web_token_data';
        $sessionKey = 'user_info';
        $session->remove($key);
        $session->remove($sessionKey);
    }
}